package io.terminus.spzx.cart.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.cart.service.CartService;
import io.terminus.spzx.model.entity.h5.CartInfo;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lichunlong lcl253137@alibaba-inc.com
 * @desc 【移动端】购物车Controller
 * @date 2024-11-19
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "购物车接口")
@RequestMapping(value = "/api/order/cart")
public class CartController {

    private final CartService cartService;

    @Operation(summary = "添加购物车")
    @GetMapping("auth/addToCart/{skuId}/{skuNum}")
    public Result<Void> addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
                                  @Parameter(name = "skuNum", description = "数量", required = true) @PathVariable("skuNum") Integer skuNum) {
        cartService.addToCart(skuId, skuNum);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("auth/cartList")
    @Operation(summary = "查询购物车")
    public Result<List<CartInfo>> cartList() {
        List<CartInfo> cartInfoList = cartService.getCartList();

        return Result.build(cartInfoList, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除购物车商品")
    @DeleteMapping("auth/deleteCart/{skuId}")
    public Result<Void> deleteCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "更新购物车商品选中状态")
    @GetMapping("/auth/checkCart/{skuId}/{isChecked}")
    public Result<Void> checkCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable(value = "skuId") Long skuId,
                                  @Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "更新购物车商品全部选中状态")
    @GetMapping("/auth/allCheckCart/{isChecked}")
    public Result<Void> allCheckCart(@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked) {
        cartService.allCheckCart(isChecked);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "清空购物车")
    @GetMapping("/auth/clearCart")
    public Result<Void> clearCart() {
        cartService.clearCart();

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // OpenFeign远程调用使用，故没有使用Result对象返回
    @Operation(summary = "获取选中的购物车商品列表")
    @GetMapping(value = "/auth/getAllChecked")
    public List<CartInfo> getAllChecked() {
        return cartService.getAllChecked();
    }

    @GetMapping(value = "/auth/deleteChecked")
    public Result<Void> deleteChecked() {
        cartService.deleteChecked();

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
